/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kurs;

/**
 *
 * @author Matrix
 */
public class DynStack implements Stack {

    protected BaseNode top;
    
    protected int size;
    
    public DynStack() {
        top = null;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return (top == null);
    }

    public BaseNode top() throws EmptyException {
        if (isEmpty()) throw new EmptyException("Stack is empty, try to push anything");
        return top;
    }

    public void push(BaseNode node) {
        BaseNode temp;
        temp = top;
        top = node;
        node.link = temp;
        size += 1;
    }

    public BaseNode pop() {
        if (isEmpty()) throw new EmptyException("Stack is empty, try to push anything");
        BaseNode temp;
        temp = top;
        top = temp.link;
        size -= 1;
        return temp;
    }

    public String printState() throws EmptyException {
        if (isEmpty()) throw new EmptyException("Stack is empty, try to push anything");
        BaseNode temp = top;
        String output = new String();
        while (temp != null) {
            //System.out.print(temp.info + " ");
            output += temp.getInfo() + " ";
            temp = temp.link;
        }
        //System.out.println("");
        return output;
    }

    public BaseNode find(String info) throws EmptyException, NotFoundException {
        if (isEmpty()) throw new EmptyException("Stack is empty, try to push anything");
        BaseNode temp = top;
        while ((temp != null)&&(!temp.getInfo().equals(info))) {
            temp = temp.link;
        }
        if (temp == null) throw new NotFoundException("Element not found");
        return temp;
    }

}
